use log::{debug, error, info, log_enabled, Level};

// extern crate mediacodec;
use mediacodec::{Frame, MediaCodec, MediaExtractor, SampleFormat, VideoFrame};

fn main() {
    // time::macros::offset!(+8);
    env_logger::init();
    test_log();

    let a = 1;
    let b = 2;
    info!("{}+{}={}", a, b, foo::my_add(a, b));

    // let r = call_dynamic().expect("Calling dynamic failed.");
    // info!("The dynamically return value: {}", r);
    //
    #[cfg(target_os = "android")]
    test_mediacodec();
}

// fn call_dynamic() -> Result<u32, Box<dyn std::error::Error>> {
//     unsafe {
//         let foo_lib_path = "/home/guoshunw/CLionProjects/hello_rust/foo/target/debug/libfoo.so";
//         let lib = libloading::Library::new(foo_lib_path)?;
//         // let func: libloading::Symbol<unsafe extern "C" fn() -> u32> = lib.get(b"say_hello")?;
//         // Ok(func())
//         let func: libloading::Symbol<unsafe extern "C" fn()> = lib.get(b"say_hello_without_ret")?;
//         func();
//         Ok(0)
//     }
// }

fn test_log() {
    // use chrono::Utc;
    // use env_logger::Builder;
    // use log::LevelFilter;
    // use std::io::Write;

    // log time zone is utc: https://stackoverflow.com/questions/59873483/how-do-i-print-logs-in-ist-timezone-using-env-logger-crate-in-rust
    // Builder::new()
    //     .format(|buf, record| {
    //         let dt = Utc::now(); // Current time in UTC
    //
    //         // Convert to Shanghai Standard Time (UTC+08:00)
    //         let ist = dt.with_timezone(&chrono::FixedOffset::east_opt(8 * 3600).unwrap());
    //
    //         writeln!(
    //             buf,
    //             "{} [{}] - {}",
    //             ist.format("%Y-%m-%dT%H:%M:%S"),
    //             record.level(),
    //             record.args()
    //         )
    //     })
    //     .filter(None, LevelFilter::Debug)
    //     .init();

    debug!("this is a debug {}", "message");
    error!("this is printed by default");
    if log_enabled!(Level::Info) {
        let x = 3 * 4; // expensive computation
        info!("the answer was: {}", x);
    }
}

#[cfg(target_os = "android")]
fn test_mediacodec() {
    let h264file = "/~/tmp/test.h264";
    let extractor = MediaExtractor::from_url(h264file).unwrap();
    info!("Track count: {}", extractor.track_count());
}
// for android test:
// adb push target/aarch64-linux-android/debug/hello_rust /data/user  && adb shell RUST_LOG=debug /data/user/hello_rust
